// index.js
const http = require("../../api/http")
Page({
  data: {
    userInfo: {
      avatarUrl: '',
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  save() {
    const userinfo = this.data.userInfo
    if (!userinfo || !userinfo.nickName) {
      wx.showToast({
        title: '昵称不能为空',
        icon: "error"
      })
    } else {
      http.post("/wx/saveUser", { nickName: userinfo.nickName }).then(() => {
        wx.switchTab({
          url: '/pages/index/index',
        })
      })
    }
  }
})